package com.nike.amoeba.adapter

import android.view.View
import android.widget.Filter
import android.widget.Filterable
import androidx.core.content.ContextCompat
import com.blankj.utilcode.util.StringUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.flyco.roundview.RoundTextView
import com.nike.amoeba.R
import com.nike.amoeba.bean.db.TaskStockBean
import com.nike.amoeba.ext.setAdapterAnimation
import com.nike.amoeba.util.SettingUtil

/**
 * @Author nike
 * @Date 2023/7/7 17:05
 * @Description
 */
class TaskStockAdapter (data: ArrayList<TaskStockBean>) :
    BaseQuickAdapter<TaskStockBean, BaseViewHolder>(
        R.layout.i_task_asset, data), Filterable {

    init {
        setAdapterAnimation(SettingUtil.getListMode())
    }

    override fun convert(viewHolder: BaseViewHolder, bean: TaskStockBean) {
        //赋值
        mFilterList[viewHolder.layoutPosition].run {
            viewHolder.setText(R.id.tv_title, OrderNo + " | " + OrderName)
            viewHolder.setText(R.id.tv_text1, context.getString(R.string.start_date) + "：" + StartDate)
            viewHolder.setText(R.id.tv_text2, context.getString(R.string.end_date) + "：" + EndDate)
            viewHolder.setText(R.id.tv_text3, context.getString(R.string.progress) + "：")
            viewHolder.setText(R.id.tv_text4, Inventory_progress)
            viewHolder.setText(R.id.tv_text5, context.getString(R.string.remarks) + "：" + Remarks)
            viewHolder.setGone(R.id.tv_text5, if (StringUtils.isEmpty(Remarks)) true else false)

            var tv_state = viewHolder.getView<RoundTextView>(R.id.tv_state)
            tv_state.visibility = View.VISIBLE
            val delegate = tv_state.delegate
            viewHolder.setText(R.id.tv_state, "STOCK")
            delegate.backgroundColor = ContextCompat.getColor(context, R.color.orange_FF8D1A)
        }
    }

    var mFilterList = ArrayList<TaskStockBean>()

    fun appendList(list: List<TaskStockBean>) {
        data = list as MutableList<TaskStockBean>
        //这里需要初始化filterList
        mFilterList = list as ArrayList<TaskStockBean>
    }

    override fun getFilter(): Filter {
        return object : Filter() {
            //执行过滤操作
            override fun performFiltering(charSequence: CharSequence): FilterResults {
                val charString = charSequence.toString()
                if (charString.isEmpty()) {
                    //没有过滤的内容，则使用源数据
                    mFilterList = data as ArrayList<TaskStockBean>
                } else {
                    val filteredList: MutableList<TaskStockBean> = ArrayList()
                    for (i in data.indices) {
                        val bean = data[i]
                        if (bean.OrderNo?.contains(charString, ignoreCase = true) == true
                        ) {
                            filteredList.add(bean)
                        }
                    }
                    mFilterList = filteredList as ArrayList<TaskStockBean>
                }
                val filterResults = FilterResults()
                filterResults.values = mFilterList
                return filterResults
            }

            //把过滤后的值返回出来
            override fun publishResults(charSequence: CharSequence, filterResults: FilterResults) {
                mFilterList = filterResults.values as ArrayList<TaskStockBean>
                notifyDataSetChanged()
            }
        }
    }


    override fun getItemCount(): Int {
        return mFilterList.size
    }

    override fun hashCode(): Int {
        return mFilterList.hashCode()
    }

}